<?php
/**
 * 渠道商信息入口
 *
 */
class Controller_Vendor extends Controller_Template
{
    public function __construct($request, $response)
    {
        $this->template = 'smarty:vendor/info';
        parent::__construct($request, $response);

        $this->_vendor      = user::current_user();
        $this->_vendor_info = $this->_vendor->get_vendor_info ();
    }

    public function action_index ()
    {
        if (! empty($this->_vendor_info))
            $this->action_info ();
        else
            $this->action_edit ();

   }

    public function action_info ()
    {
        $this->template->set('info', $this->_vendor_info);
    }

    public function action_edit ()
    {
        $this->template->set_filename('smarty:vendor/edit');
        if (! empty($this->_vendor_info))
            $this->template->set('info', $this->_vendor_info);

        try 
        {
            $post = $this->request->post();
            if (! empty($post))
            {
                $type = Arr::get($post, 'type', 'personal');
                $type = Kohana::config('user.vendor.type.'.$type);
                $type = NULL === $type ? 0 : $type;

                $name = Arr::get($post, 'name', '');
                $card = $this->save_upload_file('card', Kohana::config('upload.dir.vendor'), Kohana::config('upload.webdir.vendor'));

                $bank_name         = Arr::get($post, 'bank_name', '');
                $bank_account_no   = Arr::get($post, 'bank_account_no', '');
                $bank_account_name = Arr::get($post, 'bank_account_name', '');

                $info = array(
                    'vtype'             => $type,
                    'name'              => $name,
                    'card'              => $card,
                    'bank_name'         => $bank_name,
                    'bank_account_no'   => $bank_account_no,
                    'bank_account_name' => $bank_account_name,
                );

                if (empty($info['card']))
                    unset($info['card']);

                $this->_vendor->edit_vendor_info($info);

                $this->_vendor_info = $this->_vendor->get_vendor_info();
                $this->template->set('info', $this->_vendor_info);
                $this->template->set('alert_message', "修改完成");
            }
        }
        catch (Exception $e)
        {
            $this->template->set('alert_message', $e->getMessage());
        }
    }

    private function save_upload_file ($input_name, $upload_dir, $web_dir)
    {
        $upfile = Arr::get($_FILES, $input_name);
        if (empty($upfile) || empty($upfile['name']))
            return NULL;

		$items   = explode('.', $upfile['name']);
        $suffix  = $items[count($items) - 1];
        $sufix   = strtolower($suffix);
        if (! in_array($suffix, array('jpg', 'png', 'bmp', 'jpeg')))
        {
            return NULL;
        }
        $newname = md5(user::current_user_id().$upfile['name'].time()).'.'.$suffix;
		Upload::save($upfile, $newname, $upload_dir);
        return "$web_dir/$newname";
    }
    
    /**
     * 用户信息修改
     *
     */
    public function action_info_edit()
    {
        $post = $this->request->post();
        $uid = User::current_user_id();
        $user_contact = User_Contact::get($uid);
        
        $this->template->set('user_contact', $user_contact);
        $this->template->set_filename('smarty:vendor/info_edit');
        
        if (!empty($post))
        {
            $info['mobile'] = trim(Arr::get($post, 'mobile', ''));
            $info['email']  = trim(Arr::get($post, 'email', ''));

            try 
            {
                if ($info['mobile'] && (strlen($info['mobile']) != 11 || !is_numeric($info['mobile'])))
                    throw new Exception_Invalid("手机号码格式错误");

                if ($info['email'] && !preg_match('/^\w+\@(([a-z0-9\-])+\.)+[a-z]+$/i', $info['email']))
                    throw new Exception_Invalid("邮箱格式错误");

                
                if (! User_Contact::set($uid, $info))
                    throw new Exception_Invalid("信息没有修改");

                $this->template->set('user_contact', $info);
                $this->template->set('alert_message', "修改信息成功!");
            }
            catch(Exception $e)
            {
                $this->template->set('alert_message', $e->getMessage());
            }
        }
    }
}
